import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store/index'

Vue.use(VueRouter)

const routerPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(error => error)
}

const routes = [
  // {
  //   path: '/about',
  //   name: 'About',
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
  {
    path:'/',
    name:'Index',
    component:()=>import('../views/Index.vue'),
    meta:{
      title:"首页",
      tabBar:true,
    }
  },
  {
    path:'/category',
    name:'category',
    component:()=>import('../views/category'),
    meta:{
      title:"商品分类",
      tabBar:true,
    }
  },
  {
    path:'/cart',
    name:'cart',
    component:()=>import('../views/cart'),
    meta:{
      title:"购物车",
      tabBar:true,
      login:true,
    }
  },
  {
    path:'/user',
    name:'user',
    component:()=>import('../views/user'),
    meta:{
      title:"个人中心",
      tabBar:true,
      login:true,
      isShow:true,
    },
    children:[
      {
        path:'get_coupon',
        name:'get_coupon',
        component:()=>import('../views/coupon'),
        meta:{
          title:"领取优惠劵",
          login:true,
          isShow:false,
        }
      },
      {
        path:'add_manage',
        name:'add_manage',
        component:()=>import('../views/add_manage'),
        meta:{
          title:"地址管理",
          login:true,
          isShow:false,
        }
      },
      {
        path:'add_address',
        name:'add_address',
        component:()=>import('../views/add_address'),
        meta:{
          title:"添加地址",
          login:true,
          isShow:false,
        }
      },
      {
        path:'sign',
        name:'sign',
        component:()=>import('../views/sign'),
        meta:{
          title:"签到",
          login:true,
          isShow:false,
        }
      },
      {
        path:'sign_record',
        name:'sign_record',
        component:()=>import('../views/sign_record'),
        meta:{
          title:"签到记录",
          login:true,
          isShow:false,
        }
      },
      {
        path:'user_data',
        name:'user_data',
        component:()=>import('../views/user_data'),
        meta:{
          title:"个人资料",
          login:true,
          isShow:false,
        }
      },
      {
        path:'user_coupon',
        name:'user_coupon',
        component:()=>import('../views/user_coupon'),
        meta:{
          title:"我的优惠劵",
          login:true,
          isShow:false,
        }
      }
    ]
  },
  {
    path:'/login',
    name:'login',
    component:()=>import('../views/login'),
    meta:{
      title:"登录"
    },
    alias:'/retrieve_password'
  },
  {
    path:'/hot_new_goods',
    name:'hot_new_goods',
    component:()=>import('../views/hot_new_goods'),
    meta:{
      title:"精品推荐"
    },
    alias:'/promotion'
  },
  {
    path:'/search',
    name:'search',
    component:()=>import('../views/Search'),
    meta:{
      title:"搜索商品"
    },
  },
  {
    path:'/news_list',
    name:'news_list',
    component:()=>import('../views/news_list'),
    meta:{
      title:"新闻"
    },
  },
  {
    path:'/goods_list',
    name:'goods_list',
    component:()=>import('../views/goods_list'),
    meta:{
      title:"商品列表"
    }
  },
  {
    path:'/detail',
    name:'detail',
    component:()=>import('../views/detail'),
    meta:{
      title:"商品详情"
    }
  },
  {
    path:'/evaluate_list',
    name:'evaluate_list',
    component:()=>import('../views/evaluate_list'),
    meta:{
      title:"商品评分",
      login:true,
    }
  },
  {
    path:'/collection',
    name:'collection',
    component:()=>import('../views/collection'),
    meta:{
      title:"商品收藏",
      login:true,
    }
  },
  {
    path:'/order',
    name:'order',
    component:()=>import('../views/order'),
    meta:{
      title:"订单",
      login:true,
    },
    children:[
      {
        path:'submit',
        name:'submit',
        component:()=>import('../views/submit'),
        meta:{
          title:"提交订单",
          login:true,
        }
      },
      {
        path:'list',
        name:'Orderlist',
        component:()=>import('../views/Orderlist'),
        meta:{
          title:"我的订单",
          login:true,
        }
      },
      {
        path:'refund_list',
        name:'refund_list',
        component:()=>import('../views/refund_list'),
        meta:{
          title:"退货列表",
          login:true,
        }
      },{
        path:'status',
        name:'status',
        component:()=>import('../views/OrderStatus'),
        meta:{
          title:"订单创建成功",
          login:true,
        }
      },
      {
        path:'detail',
        name:'detail',
        component:()=>import('../views/OrderDetail'),
        meta:{
          title:"订单详情",
          login:true,
        }
      },
      {
        path:'refund',
        name:'refund',
        component:()=>import('../views/OrderRefund'),
        meta:{
          title:"申请退货",
          login:true,
        }
      }
    ]
  },
  {
    path:'/activity/goods_seckill',
    name:'goods_seckill',
    component:()=>import('../views/goods_seckill'),
    meta:{
      title:"限时抢购",
    }
  },
  {
    path:'/change_password',
    name:'change_password',
    component:()=>import('../views/change_password'),
    meta:{
      title:"修改密码",
      login:true,
    }
  },
  {
    path:'/customer/list',
    name:'customer',
    component:()=>import('../views/customer'),
    meta:{
      title:"客服中心",
      login:true,
    }
  }
]

const router = new VueRouter({
  routes,
  scrollBehavior () {
    return { x: 0, y: 0 }
  }
})

router.beforeEach((to, from, next) => {
  to.meta.title && (document.title = to.meta.title);
  if(to.meta.login && !store.getters['isLoginInfo']){
    store.state.data.interface = to.path
    next('/login');
  }else{
    next();
  }
});

export default router
